<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-button class="btn-home" type="info" @click="logout">退出</el-button>
    <el-header class="home-header">
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
    </el-header>
    <!-- 内容主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside class="home-aside" :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边菜单 -->
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            :key="item.id"
            v-for="item in menulist"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 字体图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span v-text="item.authName"></span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              :key="subItem.id"
              v-for="subItem in item.children"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span v-text="subItem.authName"></span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main class="home-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      menulist: [],
      // 字体图标
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: this.$route.path
    }
  },
  // 页面刷新自动加载侧边栏
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  // 点击模块常亮
  beforeUpdate () {
    this.activePath = this.$route.path === '/goods/add' ? '/goods' : this.$route.path
  },
  methods: {
    // 退出按钮功能
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边栏
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },
    // 侧边栏显示状态
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    }
    // 点击模块常亮

    // saveNavState(activePath) {
    //   window.sessionStorage.setItem("activePath", activePath);
    //   this.activePath = activePath;
    // },
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
  .btn-home {
    position: absolute;
    top: 10px;
    right: 20px;
  }
  .home-header {
    display: flex;
    background-color: #373d41;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    font-size: 20px;
    color: #fff;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
    }
  }
  .home-aside {
    background-color: #333744;
    .iconfont {
      margin-right: 10px;
    }
    .el-menu {
      border-right: none;
    }
    .toggle-button {
      background-color: #4a5064;
      text-align: center;
      color: #fff;
      line-height: 24px;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  }
  .home-main {
    background-color: #eaedf1;
  }
}
</style>
